***************************************************************
*This file cleans financial aid of institutions from the IPEDS*
***************************************************************

*** create dta files from csv, labeling
cd "$raw_data_education/financialAid"

foreach i in 1999 {
cap do "$do_clean_education/IPEDS - do/do financialAid/sfa`i's.do"
keep unitid fedgrntp fedgrnta stgrntp stgrnta ingrntp ingrnta loanpct loanavg
	foreach v in fedgrntp fedgrnta stgrntp stgrnta ingrntp ingrnta loanpct loanavg {
	replace `v' = . if `v' < 0 
	}
egen avgAidAmount = rowmean(fedgrnta stgrnta ingrnta loanavg)
label variable avgAidAmount "averege amount of financial aid received from any source (undergraduate)"
drop fedgrnta stgrnta ingrnta loanavg
gen year = `i'
save "$clean_data_education/financialAid_`i'.dta", replace	
clear
}
************************************************************

foreach i in 2000 2001 2006 2007 2008 2013 2014 2015 {
cap do "$do_clean_education/IPEDS - do/do financialAid/sfa`i's.do"
keep unitid scfa1n loan_n loan_p loan_a anyaidn anyaidp fgrnt_a sgrnt_a igrnt_a 
egen avgAidAmount = rowmean(fgrnt_a sgrnt_a igrnt_a loan_a)
label variable avgAidAmount "averege amount of financial aid received from any source (undergraduate)"
drop fgrnt_a sgrnt_a igrnt_a loan_a
gen year = `i'
save "$clean_data_education/financialAid_`i'.dta", replace	
clear
}
************************************************************

* append
use "$clean_data_education/financialAid_1999.dta", clear
foreach i in 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/financialAid_`i'.dta"
rm "$clean_data_education/financialAid_`i'.dta"
}
rm "$clean_data_education/financialAid_1999.dta"

* final cleanings
do "$do_clean_education/clean institutions - id.do"

*** Build three year averages
* year group
gen yearGroup = 0
replace yearGroup = 1990 if year <= 1991 
replace yearGroup = 1994 if year >= 1993 & year <= 1995
replace yearGroup = 2000 if year >= 1999 & year <= 2001
replace yearGroup = 2007 if year >= 2006 & year <= 2008
replace yearGroup = 2014 if year >= 2013

**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
collapse (mean) fedgrntp stgrntp ingrntp loanpct avgAidAmount scfa1n anyaidn anyaidp loan_n loan_p, by(unitid yearGroup)

foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

save "$clean_data_education/financialAid.dta", replace


